﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Web;
using System.Web.Mvc;
using Maxleaf.EDI.API.Contract;
using Maxleaf.EDI.API.Contract.Client.Commands;
using Maxleaf.EDI.API.Contract.Faults;
using Maxleaf.EDI.Client.Models;
using Maxleaf.EDI.Infrastructure;

namespace Maxleaf.EDI.Client.Controllers
{
    public class UserController : Controller
    {
        private readonly IClientService ClientService;

        public UserController()
        {
            this.ClientService = TinyIoC.TinyIoCContainer.Current.Resolve<IClientService>();
        }

        [Authorize]
        public ActionResult Index()
        {
            return View();
        }

        [Authorize]
        public ActionResult ResetPassword()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult ResetPassword(string oldpassword, string password)
        {
            var userName = this.User.Identity.Name;
            try
            {   
                this.ClientService.ResetPassword(
                    new ResetPasswordCommand()
                    {
                        NewHashPassword = MD5Helper.GetHashedPassword(userName, password),
                        OldHashPassword = MD5Helper.GetHashedPassword(userName, oldpassword),
                        UserName = userName
                    });
            }
            catch (Exception ex)
            {
                return this.Json(new { Success = false, Msg = "重置密码失败" });
            }

            return this.Json(new { Success = true, UserName = userName });
        }
    }
}
